package org.practice.action;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 单个文件上传（多文件上传用数组封装）
 * Struts2使用开源项目Apache Jakarta (commons-io.jar,commons-fileupload.jar)
 * Commons FileUpload和内建的FileUploadInterceptor拦截器实现文件上传
 */
public class UploadFileAction extends ActionSupport{
	private File file;//file属性用来上传文件,struts2就将文件保存到web服务器的临时目录为一个.temp文件
	//下面两个属性命名有规范
	private String fileContentType;
	private String fileFileName;
	@Override
	public String execute(){
		System.out.println("开始上传单个文件-----------------------");
		//E:\Workspaces\MyEclipse 8.5\Struts2_Spring\WebRoot\\upload
		String filePath=ServletActionContext.getServletContext().getRealPath("/upload");
        System.out.println("==========" + filePath);
        System.out.println("==========" + getFileFileName());
        System.out.println("==========" + getFileContentType());
        System.out.println("==========" + getFile());
		readUploadTextFile(getFile());
		System.out.println("execute UploadFileAction....."+file.getName());
		return SUCCESS;
	}
	private static void readUploadTextFile(File file){
    	BufferedReader br=null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
			//br = new BufferedReader(new FileReader(CSV_FILE));
			String line="";
			while((line=br.readLine())!=null){
				System.out.println(line);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    	
    }
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
}
